package P087_checkVauleOf;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @Description : 适用vauleOf前必须进行校验
 **/
public class CheckValueOf {


    @Test
    public void test() {

        List<String> params = Arrays.asList("Spring", "summer");
        for (String name : params) {
            Season s = Season.valueOf(name);
            if (s != null) {
                System.out.println(s);
            } else {
                System.out.println("无");
            }
        }
    }

    /**
     * 修改一： try...catch...
     */
    @Test
    public void test1() {

        List<String> params = Arrays.asList("Spring", "summer");
        for (String name : params) {
            try {
                Season s = Season.valueOf(name);
                if (s != null) {
                    System.out.println(s);
                } else {
                    System.out.println("无");
                }
            } catch (Exception e) {
                System.out.println();
            }
        }

    }

    @Test
    public void test2() {

        List<String> params = Arrays.asList("Spring", "summer");
        for (String name : params) {
            if(!Season1.contains(name)){
                continue;
            }
            Season1 s = Season1.valueOf(name);
            if (s != null) {
                System.out.println(s);
            } else {
                System.out.println("无");
            }
        }
    }


    enum Season {
        Spring, Summer, Autumn, Winter;

        public static Season getComfortableSeason() {
            return Spring;
        }
    }

    /**
     * 方法二：扩展枚举
     */
    enum Season1 {
        Spring, Summer, Autumn, Winter;

        public static boolean contains(String name){
            Season1[] season =values();
            for(Season1 s:season){
                if(s.name().equals(name)){
                    return true;
                }
            }
            return false;
        }
    }


}
